dojo.provide('app.model');
(function() {
	function makeClass(func, props, proto) {
		func.prototype = getProto(props, proto);
		return func;
	}
	function getProto(props, proto) {
		var ret = typeof proto === 'function' ? new proto() : {};
		for (idx in props) {
			if (typeof props[idx] === 'object') {
				for (objp in props[idx]) {
					for (objvals in props[idx][objp]) {
						if (typeof props[idx][objp][objvals] !== 'function') {
							ret[objp + '.' + objvals] = null;
						}
					}
				}
			} else {
				ret[props[idx]] = null;
			}
		}
		return ret;
	}

	var model = makeClass(function() {
		this.flatten = function(obj, prefix) {
			dojo.mixin(this, app.model.prune(obj, prefix));
		};
	}, [ 'id', 'version' ]);

	app.model.place = makeClass(function() {
	}, [ 'description' ], model);
	var _plc = new app.model.place();
	app.model.geo = makeClass(function() {
	}, [ 'json', 'lat', 'lng', 'centerLat', 'centerLng' ], app.model.place);
	app.model.country = makeClass(function() {
	}, [ 'isoCode', 'countryCode' ], app.model.geo);
	var _ctr = new app.model.country();
	app.model.contact = makeClass(function() {
	}, [ 'name', 'email', 'homePhone', 'commercialPhone', {
		'country' : _ctr
	} ], model);
	app.model.address = makeClass(function() {
	}, [ 'stNumber', 'complement', 'zip', {
		'city' : _plc
	}, {
		'state' : _plc
	}, {
		'country' : _ctr
	}, {
		'district' : _plc
	}, {
		'street' : _plc
	} ], model);
	app.model.register = makeClass(function() {
	}, [ {
		'contact' : new app.model.contact()
	}, {
		'address' : new app.model.address()
	}, 'confirmed', 'comments', 'type' ], model);

	Register = new app.model.register();

	/*
	 * var model = dojo.declare(null,null,{ id:null, version:null,
	 * flatten:function(obj,prefix){ dojo.mixin(this,
	 * app.model.prune(obj,prefix)); } }); var place =
	 * dojo.declare(null,[model],{ description:null }); var geo =
	 * dojo.declare(null,[place],{ json:null, lat:null, lng:null,
	 * centerLat:null, centerLng:null }); var country =
	 * dojo.declare(null,[geo],{ isoCode:null, countryCode:null });
	 * 
	 * var contact = dojo.declare(null,[model],{
	 * 
	 * });
	 */

})();

app.model.prune = function(obj, prefix) {
	var ret = {};
	if (obj === null || typeof obj === 'undefined' || obj.__visited)
		return;// avoid loops
	if (typeof obj === 'string' || typeof obj === 'number') {
		ret[prefix] = obj;
		return ret;
	} else if (typeof obj === 'object') {
		var visitProp = '__visited';
		obj[visitProp] = true;
		for (a in obj) {
			if (a !== visitProp) {
				dojo.mixin(ret, app.model.prune(obj[a], (prefix ? prefix + '.'
						: '')
						+ a));
			}
		}
		delete obj[visitProp];
		return ret;
	} else
		return null;
};
app.model.getData = function(wdgs, hidds) {
	console.group('getData');
	var send = {}, obj, id;
	console.group('Widgets');
	for (a in wdgs) {

		id = wdgs[a];
		obj = dijit.byId(id);
		var data;
		if (obj) {
			console.group(obj.get('name'));
			console.log(obj.domNode);
			if (obj.item) {
				console.log(obj.item);
				var prop = obj.get('name');
				var idx = prop.lastIndexOf(".");
				idx = idx < 0 ? prop.length : idx;
				var pck = prop.substring(0, idx);
				data = app.model.prune(obj.item.i, pck);
				console.log(data);
			} else {
				data = typeof this.typeCtor == 'function' ? new this.typeCtor()
						: {};
				var val = obj.get('value');
				data[obj.get('name')] = val != undefined && val != null ? val
						: "";
			}
			console.log(data);
			console.log(dojo.mixin({}, dojo.mixin(send, data)));
			console.groupEnd();

		}
	}
	console.groupEnd();
	console.group('hidden');
	for (idx in hidds) {
		id = hidds[idx];
		obj = dojo.byId(id);
		if (obj) {
			console.group(obj.name);
			console.log(obj);
			data = {};
			var val = dojo.attr(obj, 'value');
			data[obj.name] = val != undefined && val != null ? val : "";
			console.log(data)
			console.log(dojo.mixin({}, dojo.mixin(send, data)));
			console.groupEnd();
		}
	}
	console.groupEnd();
	console.groupEnd();
	return send;
}